# -*- encoding: utf-8 -*-

import json
from Common.mysql import app
from flask import jsonify, request
from src.Bugs.Function.bug_count import BugCount
from src.User.token import token_verify, before_request
from Common.yaml_method import YamlMethod
from src.Bugs.Function.create_bug import CreateBug
from src.Bugs.Function.bug_list import BugList
from src.Bugs.Function.update_bug import UpdateBug
from src.Bugs.Function.bug_comment import BugComment
from src.Bugs.Function.delete_bug import DeleteBug
from src.Bugs.Function.bug_history import BugHistory

code = YamlMethod().read_data('code.yaml')['code']


@app.route('/bug/create', methods=['POST'])
@token_verify
def create_bug(create_user):
    """
    创建缺陷接口
    :param create_user:
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    title = data['title']
    describe = data['describe']
    project_id = data['projectId']
    sprint = data['sprint']
    severity = data['severity']
    level = data['level']
    handler = data['handler']
    if isinstance(handler, list):
        handler = ', '.join(handler)
    # responsible = data['responsible']
    # if isinstance(responsible, list):
    #     responsible = ', '.join(responsible)
    bug_type = data['bugType']
    platform = data['platform']

    # 校验参数
    if not all([title, str(describe), create_user]):
        return jsonify(code=code[7], msg="信息不完整")

    # 提交保存测试用例数据
    res = CreateBug().create_bug(title, describe, project_id, sprint, severity, level, handler, handler, bug_type,
                                 platform, create_user)
    return res


@app.route('/bug/list', methods=['POST'])
@before_request
def bug_list():
    """
    获取缺陷列表接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    page = data['page']
    limit = data['limit']
    title = data['bugTitle']
    project_id = data['projectId']
    sprint = data['sprint']
    platform = data['platform']
    level = data['level']
    status = data['status']
    handler_data = data['handler']
    if handler_data:
        handler_value = []
        for item in handler_data:
            handler_value.append(item[1])
        handler = {
            'data_key': handler_data[0][0],
            'data_value': handler_value
        }
    else:
        handler = handler_data
    # 校验参数
    if not all([project_id]):
        return jsonify(code=code[7], msg="未选择项目")

    res = BugList().bug_list(page, limit, title, project_id, sprint, platform, level, status, handler)
    return res


@app.route('/bug/update', methods=['POST'])
@token_verify
def bug_update(create_user):
    """
    更新缺陷接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    bug_id = data['bug_id']
    title = data['title']
    describe = data['describe']
    sprint = data['sprint']
    severity = data['severity']
    level = data['level']
    handler = data['handler']
    if isinstance(handler, list):
        handler = ', '.join(handler)
    responsible = data['responsible']
    if isinstance(responsible, list):
        responsible = ', '.join(responsible)
    bug_type = data['bugType']
    platform = data['platform']
    status = data['status']

    res = UpdateBug().update_bug(bug_id, title, describe, sprint, severity, level, handler, responsible, bug_type,
                                 platform, status)
    if res['code'] == code[0]:
        UpdateBug().insert_history(bug_id, title, describe, sprint, severity, level, handler, responsible,
                                   bug_type, platform, status, '', create_user)
    return res


@app.route('/bug/comment', methods=['POST'])
@token_verify
def bug_comment(create_user):
    """
    缺陷评论接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    bug_id = data['bug_id']
    comment = data['comment']

    res = UpdateBug().insert_comment(bug_id, comment, create_user)
    if res['code'] == code[0]:
        UpdateBug().insert_history(bug_id, '', '', '', '', '', '', '', '', '',
                                   '', comment, create_user)
    return res


@app.route('/bug/commentList', methods=['POST'])
@before_request
def bug_comment_list():
    """
    获取缺陷评论接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    bug_id = data['bug_id']

    res = BugComment().bug_comment(bug_id)
    return res


@app.route('/bug/delete', methods=['POST'])
@token_verify
def bug_delete(update_user):
    """
    删除缺陷接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    bug_id = data['bug_id']

    res = DeleteBug().delete_bug(bug_id, update_user)
    return res


@app.route('/bug/history', methods=['POST'])
@before_request
def bug_history():
    """
    获取缺陷操作日志接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    bug_id = data['bug_id']

    res = BugHistory().bug_history(bug_id)
    return res


@app.route('/bug/base_data', methods=['POST'])
@before_request
def bug_base_data():
    """
    获取缺陷统计-基础数据接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    project_id = data['projectId']
    sprint = data['sprint']

    res = BugCount().base_bug_data(project_id, sprint)
    return res


@app.route('/bug/bug_type_user', methods=['POST'])
@before_request
def bug_type_user():
    """
    获取缺陷统计-缺陷类型接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    project_id = data['projectId']
    sprint = data['sprint']
    responsible = data['responsible']

    res = BugCount().development_bug_type(project_id, sprint, responsible)
    return res


@app.route('/bug/daily_bug', methods=['POST'])
@before_request
def daily_bug():
    """
    获取缺陷统计-每日缺陷接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    project_id = data['projectId']
    sprint = data['sprint']

    res = BugCount().count_daily_bug(project_id, sprint)
    return res


@app.route('/bug/developer_bug', methods=['POST'])
@before_request
def developer_bugs():
    """
    获取缺陷统计-开发人员的缺陷统计接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    project_id = data['projectId']
    sprint = data['sprint']
    development = data['development']

    res = BugCount().developer_bugs(project_id, sprint, development)
    return res